package com.pactera.madp.cp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.cp.api.em.workupdatetime.TypeEnum;
import com.pactera.madp.cp.api.entity.*;
import com.pactera.madp.cp.mapper.CorpDayMapper;
import com.pactera.madp.cp.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @description:首页数据
 * @author: Huayu
 * @time: 2021/4/14 13:33
 */
@Service
public class CorpDataServiceImpl extends ServiceImpl<CorpDayMapper, CorpDayEntity> implements ICorpDataService {

    @Autowired
    private ICorpService corpServiceImpl;
    @Autowired
    private IWorkContactEmployeeService workContactEmployeeServiceImpl;
    @Autowired
    private IWorkRoomService workRoomServiceImpl;
    @Autowired
    private IWorkContactRoomService workContactRoomServiceImpl;
    @Autowired
    private IWorkUpdateTimeService workUpdateTimeServiceImpl;
    private final static Logger logger = LoggerFactory.getLogger(CorpDataServiceImpl.class);

    @Override
    public void handle() {
        //查询所有企业
        List<CorpEntity> corpEntityList = corpServiceImpl.getCorps("id");
        //处理企业数据
        for (CorpEntity corpEntity:
                corpEntityList) {
            //计算企业当日新增数据 并插入日数据表
            handleDayData(corpEntity.getCorpId());
            //记录更新时间
            updateTime(corpEntity.getCorpId());
        }
    }

    @Override
    public CorpDayEntity getCorpDayDataByCorpIdDate(Integer corpId, Date time) {
        QueryWrapper<CorpDayEntity> corpDataEntityWrapper = new QueryWrapper();
        corpDataEntityWrapper.getSqlSelect();
        corpDataEntityWrapper.eq("corp_id",corpId);
        corpDataEntityWrapper.eq("date",time);
        return this.baseMapper.selectOne(corpDataEntityWrapper);
    }

    @Override
    public List<CorpDayEntity> getCorpDayDatasByCorpIdTime(Integer corpId, Date beginDate, Date endDate) {
        QueryWrapper<CorpDayEntity> corpDataEntityWrapper = new QueryWrapper();
        corpDataEntityWrapper.getSqlSelect();
        corpDataEntityWrapper.eq("corp_id",corpId);
        corpDataEntityWrapper.ge("date",beginDate);
        corpDataEntityWrapper.lt("date",endDate);
        return this.baseMapper.selectList(corpDataEntityWrapper);
    }

    @Override
    public List<CorpDayEntity> getCorpDayDatasByCorpIdDateOther(Date startDate, Date endDate, String cls) {
        QueryWrapper<CorpDayEntity> corpDataEntityWrapper = new QueryWrapper();
        corpDataEntityWrapper.select(cls);
        corpDataEntityWrapper.eq("corp_id", AccountService.getCorpId());
        corpDataEntityWrapper.ge("date",startDate);
        corpDataEntityWrapper.lt("date",endDate);
        corpDataEntityWrapper.orderByAsc("date");
        return this.baseMapper.selectList(corpDataEntityWrapper);
    }


    /**
     *
     *
     * @description:计算企业当日新增数据 并插入日数据表
     * @return:
     * @author: Huayu
     */
    private void handleDayData(Integer corpId) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(new Date());
        calendar1.set(Calendar.HOUR_OF_DAY, 0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.SECOND, 0);
        calendar1.set(Calendar.MILLISECOND, 0);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        //当日新增客户数
        Date startTime = calendar1.getTime();
        Date endTime = calendar.getTime();
        List<WorkContactEmployeeEntity> contact = workContactEmployeeServiceImpl.countWorkContactEmployeesByCorpIdTime(corpId,startTime,endTime);
        //今日新增社群数
        List<WorkRoomEntity> room = workRoomServiceImpl.countAddWorkRoomsByCorpIdTime(corpId,startTime,endTime);
        //查询企业下所有的群
        List<WorkRoomEntity> WorkRoomEntityList = workRoomServiceImpl.getWorkRoomsByCorpId(corpId,"id");
        List<WorkContactRoomEntity> intoRoom = null;
        List<WorkContactRoomEntity> outRoom = null;
        if(WorkRoomEntityList != null && WorkRoomEntityList.size() > 0){
            StringBuilder sb = new StringBuilder();
            for (WorkRoomEntity workRoomEntity:
                    WorkRoomEntityList) {
                sb.append(workRoomEntity.getId()).append(",");
            }
            //当日新增入群数
            String roomIds = sb.substring(0,sb.length()-1);
            intoRoom = workContactRoomServiceImpl.countAddWorkContactRoomsByRoomIdTime(roomIds,startTime,endTime);
            //今日退群人数
            outRoom = workContactRoomServiceImpl.countQuitWorkContactRoomsByRoomIdTime(roomIds,startTime,endTime);
        }
        //今日流失客户
        List<WorkContactEmployeeEntity> lossContact = workContactEmployeeServiceImpl.countLossWorkContactEmployeesByCorpIdTime(corpId,startTime,endTime);
        CorpDayEntity corpDayEntity = new CorpDayEntity();
        corpDayEntity.setCorpId(corpId);
        corpDayEntity.setAddContactNum(contact.size());
        corpDayEntity.setAddRoomNum(room.size());
        corpDayEntity.setAddIntoRoomNum(intoRoom == null ? 0 :intoRoom.size());
        corpDayEntity.setLossContactNum(lossContact.size());
        corpDayEntity.setQuitRoomNum(outRoom == null ? 0 : outRoom.size());
        corpDayEntity.setDate(startTime);
        //查询日数据表中是否已有今日数据
        CorpDayEntity corpData = getCorpDayDataByCorpIdDate(corpId,startTime);
        if(corpData != null){
            //更新
            Integer i = updateCorpDayDataById(corpData.getId(),corpDayEntity);
            if(i < 1){
                logger.error("更新企业日数据失败>>>>>>>>>>>");
            }
        }else{
            //新增
            Integer i = createCorpDayData(corpDayEntity);
            if(i < 1){
                logger.error("新增企业日数据失败>>>>>>>>>>>");

            }
        }
    }


    private Integer createCorpDayData(CorpDayEntity corpDayEntity) {
        Integer i = this.baseMapper.insert(corpDayEntity);
        return i;
    }


    private Integer updateCorpDayDataById(Integer id, CorpDayEntity corpDayEntity) {
        UpdateWrapper<CorpDayEntity> corpDataEntityUpdateWrapper = new UpdateWrapper();
        corpDataEntityUpdateWrapper.eq("id",id);
        int i = this.baseMapper.update(corpDayEntity,corpDataEntityUpdateWrapper);
        return i;
    }


    /**
     *
     *
     * @description:记录更新时间
     * @return:
     * @author: Huayu
     */
    private void updateTime(Integer corpId) {
        //查询当前企业有没有同步客户的时间
        WorkUpdateTimeEntity workUpdateTimeEntity = workUpdateTimeServiceImpl.getWorkUpdateTimeByCorpIdType(corpId, TypeEnum.CORP_DATA.getCode());
        if(workUpdateTimeEntity != null){
            workUpdateTimeEntity.setLastUpdateTime(new Date());
            Integer id = workUpdateTimeEntity.getId();
            Integer i = workUpdateTimeServiceImpl.updateWorkUpdateTimeById(id,workUpdateTimeEntity);
            if(i < 1){
                throw new CheckedException("更新企业日数据时间失败");
            }
        }else{
            //如果没有新增
            WorkUpdateTimeEntity workUpdateTimeEntity1 = new WorkUpdateTimeEntity();
            workUpdateTimeEntity1.setCorpId(corpId);
            workUpdateTimeEntity1.setType(TypeEnum.CORP_DATA.getCode());
            workUpdateTimeEntity1.setLastUpdateTime(new Date());
            Integer i = workUpdateTimeServiceImpl.createWorkUpdateTime(workUpdateTimeEntity1);
            if(i < 1){
                throw new CheckedException("添加企业日数据时间失败");
            }
        }
    }

}
